/**
 * Created by WT-SUN on 2017/4/18.
 */
$(function () {
    $('#disTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/occDiseases/disTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'diseasesName', title: '职业病名称', width: 100, sortable: true},
            {field: 'diseasesTypeName', title: '职业病类型', width: 100},
            {field: 'diseasesCode', title: '职业病编码', width: 100},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'addTime', title: '登记时间', width: 100, sortable: true},
            {field: 'deptName', title: '登记部门', width: 100}
        ]]
    });
    queryComboData();
    $('#btn_occdis_add').on('click',function () {
        addOccDis();
    });
    $('#btn_occdis_delete').on('click',function () {
        deleteOccDis();
    });
    $('#btn_occdis_edit').on('click',function () {
        editOccDis();
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm').form('reset');
    });
});

function queryComboData() {
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_OCCDISEASES_TYPE;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $('#diseasesType').combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#disTb').datagrid('clearSelections');
    var queryParams = $('#queryForm').serializeJson();
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#disTb').datagrid('options').queryParams = queryParams;
    $("#disTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/occDiseases/disTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['600px', '55%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addOccDis() {
    var title = '新增职业病';
    var content = ROOT_PATH + '/occupationalHealth/occDiseases/add.shtml';
    openLayers(title, content);
}
/**
 * 删除
 */
function deleteOccDis() {
    var rows = $('#disTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/occDiseases/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#disTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editOccDis() {
    var rows = $('#disTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return;
    }
    var id = rows[0].id;
    var title = '编辑职业病';
    var content = ROOT_PATH + '/occupationalHealth/occDiseases/info.shtml?id=' + id+'&type=edit';
    openLayers(title, content);
}

